package com.emm.yixun.basic.service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.basic.service.DialTransactionalService;
import com.emm.yixun.basic.service.FollowService;
import com.emm.yixun.basic.service.TelRecordService;
import com.emm.yixun.common.model.dto.FollowDto;
import com.emm.yixun.common.model.dto.TelRecordDto;
import com.emm.yixun.common.model.enums.FollowModeEnum;
import com.emm.yixun.common.model.enums.FollowTypeEnum;
import com.emm.yixun.common.model.enums.IsReadEnum;
import com.emm.yixun.soap.client.NetworkTelephoneClient;

@Service("dialTransactionalService")
public class DialTransactionalServiceImpl implements DialTransactionalService{
	
	@Autowired
	private TelRecordService telRecordService;
	
	@Autowired
	private FollowService  followService;
	
	/*
	 *拨打电话
     *参数范例
	 TelRecordDto trd=new TelRecordDto();
	 trd.setMerchantId(merchantId);//商户ID
	 trd.setProjectId(projectId);//项目ID
	 trd.setCreateUserId(userId);//用户ID
	 trd.setCaller(caller);//主叫号码
	 trd.setCalled(called);//被叫号码
	 trd.setCallType("0");//通话类型:0：呼出；1：呼入
	 trd.setTerminalType("0");//终端类型:0：PC；1：手机；3:座机
	 trd.setTerminalModel("0");//终端操作模块:0：客户；
	 trd.setTerminalModelId(terminalModelId);
	 trd.setUserCode("");//终端模块ID，如客户ID
	 trd.setAccessCode("");//项目编码
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public TelRecordDto call(TelRecordDto trd){
		/*
		 * 1.调用电话平台回拨指令
		 * 2.产生通话记录
        */
		
		 JSONObject object=NetworkTelephoneClient.call(trd.getCaller(), trd.getCalled());
		 String errMsg=object.getString("errMsg");
		 if(StringUtils.isNotBlank(errMsg) && !errMsg.equals("0")){
			 throw new RuntimeException(errMsg);
		 }
		 String uniqueId=object.getString("uniqueId");
		 Date now=new Date();
		 //记录通话记录
		 trd.setCallStatus("0");//通话状态:0：待获取；1：接通；2未接通
		 trd.setUniqueId(uniqueId);//唯一标示
		 trd.setCallTime(now);
		 trd.setDelFlag("0");
		 trd.setUpdateTime(now);
		 telRecordService.create(trd);
		 return trd;
    }
	/*
	 *拨打电话并生成跟进记录
     TelRecordDto参数范例
	 TelRecordDto trd=new TelRecordDto();
	 trd.setMerchantId(merchantId);//商户ID
	 trd.setProjectId(projectId);//项目ID
	 trd.setCreateUserId(userId);//用户ID
	 trd.setCaller(caller);//主叫号码
	 trd.setCalled(called);//被叫号码
	 trd.setCallType("0");//通话类型:0：呼出；1：呼入
	 trd.setTerminalType("0");//终端类型:0：PC；1：手机；3:座机
	 trd.setTerminalModel("0");//终端操作模块:0：客户；
	 trd.setTerminalModelId(terminalModelId);
	 trd.setUserCode("");//终端模块ID，如客户ID
	 trd.setAccessCode("");//项目编码
	 *
	 FollowDto参数范例
	 FollowDto fd=new FollowDto();
	 fd.setCustomerId(customerId);
	 fd.setMerchantId(merchantId);
	 fd.setProjectId(projectId);
	 fd.setProjectCode(projectCode);
	 fd.setUserCode(userCode);
	 fd.setAccessCode(accessCode);
	 fd.setCreateUserId(createUserId);
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public FollowDto callAndFollow(TelRecordDto trd,FollowDto fd){
		/*
		 * 1.拨打电话
		 * 2.生成跟进记录
        */
		 //拨打电话
		 TelRecordDto addTRD=call(trd);
		 //生成跟进记录
		 Date now=new Date();
		 fd.setTelRecordId(addTRD.getId());
		 fd.setDelFlag("0");
		 fd.setFollowContent("拨打电话自动生成跟进");
		 fd.setFollowType(FollowTypeEnum.TEXT.getNo());
		 fd.setFollowTime(now);
		 fd.setFollowMode(FollowModeEnum.GOCALL.getNo());
		 fd.setUpdateTime(now);
		 fd.setIsRead(IsReadEnum.UNREAD.getNo());
		 fd.setIsSend("0");
		 FollowDto adAdd=followService.create(fd);
		 return adAdd;
		
    }
}
